package com.longcai.cm.common.utils.aes;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.symmetric.AES;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;

/**
 * @author libingxing
 */
public class AesUtil {

    public static void main(String[] args) {
        System.err.println(IdUtil.nanoId());
    }

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    public static byte[] encode(String content) {
        final AES aes = getAes();
//        加密
        return aes.encrypt(content);
    }

    public static String encodeToHex16(String content) {
        final AES aes = getAes();
//        加密为16进制表示
        return aes.encryptHex(content);
    }

    public static byte[] decode(String content) {
//        构建
        final AES aes = getAes();
//        解密
        return aes.decrypt(content);
    }

    public static String decodeToStr(String content) {
//        构建
        final AES aes = getAes();
//        解密为字符串
        return aes.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
    }

    private static AES getAes() {
        String secret = "IByEL@cg73gL6+0V";
        String iv = "JiHQJ6I7DG)dR4X*";

        return new AES("CBC", "PKCS7Padding",
//                密钥，可以自定义
                secret.getBytes(),
//                iv加盐，按照实际需求添加
                iv.getBytes());
    }
}
